var server = require('../../../utils/server');
Page({
  data: {
    indicatorDots: false,
    vertical: false,
    autoplay: false,
    circular: false,
    interval: 2000,
    duration: 500,
    previousMargin: 0,
    nextMargin: 0,
    list: [1, 2, 3, 4, 5, 6],
    goods: {},
    current: 0,
    tabStates: [true, false, false],
    tabClasss: ["text-select", "text-normal", "text-normal"],
    galleryHeight: getApp().screenWidth,
    tab: 0,
    goods_num: 1,
    textStates: ["view-btns-text-normal", "view-btns-text-select"],
    collect: "/images/collect.png"
  },

  propClick: function(e) {
    var pos = e.currentTarget.dataset.pos;
    var index = e.currentTarget.dataset.index;
    var goods = this.data.goods
    for (var i = 0; i < goods.goods.goods_spec_list[index].length; i++) {

      if (i == pos)
        goods.goods.goods_spec_list[index][pos].isClick = 1;
      else
        goods.goods.goods_spec_list[index][i].isClick = 0;
    }

    this.setData({
      goods: goods
    });
    this.checkPrice();
  },
  addCollect: function(e) {
    var goods_id = e.currentTarget.dataset.id;
    console.log(goods_id);

    var user_id = getApp().globalData.userInfo.user_id
    var ctype = 0;


    server.getJSON('/Goods/collectGoods/user_id/' + user_id + "/goods_id/" + goods_id + "/type/" + ctype, function(res) {
      that.setData({
        collect: "/images/collected.png",
        colltext: '收藏'
      })
      wx.showToast({
        title: res.data.msg,
        icon: 'none',
        duration: 2000
      })
    });



  },
  bindMinus: function(e) {

    var num = this.data.goods_num;
    if (num > 1) {
      num--;
    }

    this.setData({
      goods_num: num
    });
  },
  bindManual: function(e) {
    var index = parseInt(e.currentTarget.dataset.index);
    var num = e.detail.value;
    this.setData({
      goods_num: num
    });
  },
  bindPlus: function(e) {
    var num = this.data.goods_num;

    num++;

    this.setData({
      goods_num: num
    });
  },
  onLoad: function(options) {
    var that = this;
    var goodsId = options.objectId;
    this.getGoodsById(goodsId);
    that.setData({
      goodsid:goodsId,
    })
    // var user_id = getApp().globalData.userInfo.user_id
    if (options.first_leader !== 0) {
      getApp().globalData.first_leader = options.first_leader;
    }
    console.log(options)
    console.log("pinuan")
    var ctype = 0;
    var user = getApp().globalData.userInfo;
    if (user !== null) {
      server.getJSON('/Goods/checkCollect/user_id/' + user.user_id + "/goods_id/" + goodsId + "/type/" + ctype, function(res) {
        if (res.data.status < 0) {
          that.setData({
            collect: "/images/collected.png",
            colltext: '收藏'
          })
        }
      });
    }
    wx.getSetting({
      success(res) {
        if (!res.authSetting['scope.userInfo']) {
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称
          wx.navigateTo({
            url: '/pages/auth/auth',
          })
        } else {
          var check = setInterval(function () {
            wx.request({
              url: 'https://www.donglongshangcheng.com/Home/Goods/check_book.html',
              data: {
                goodsid: goodsId
              },
              method: 'post',
              success: function (res) {
                if (res.status == 1) {
                  console.log("更新中")
                  // clearInterval(check);
                } else {
                  clearInterval(check);
                }
              },
              fail: function (e) {
                clearInterval(check);
              }
            })
          }, '2000')
        }
      }
    })
  },
  // onShow: function(e) {

  // },
  tabClick: function(e) {
    var index = e.currentTarget.dataset.index
    var classs = ["text-normal", "text-normal", "text-normal"]
    classs[index] = "text-select"
    this.setData({
      tabClasss: classs,
      tab: index
    })
  },
  getGoodsById: function(goodsId) {

    var that = this

    server.getJSON('/Activity/group/id/' + goodsId, function(res) {
      var goodsInfo = res.data.result;
      console.log(res.data.groupInfo)
      that.setData({
        groupInfo: res.data.groupInfo,
        goods: goodsInfo,
        goods1: res.data.goods,
        order: res.data.result.order_list
      });
      that.checkPrice();
    });
  },
  checkPrice: function() {
    this.setData({
      price: this.data.groupInfo.price
    });
    return;
    var goods = this.data.goods;
    var spec = ""
    for (var i = 0; i < goods.goods.goods_spec_list.length; i++) {

      for (var j = 0; j < goods.goods.goods_spec_list[i].length; j++) {
        if (goods.goods.goods_spec_list[i][j].isClick == 1) {
          if (spec == "")
            spec = goods.goods.goods_spec_list[i][j].item_id
          else
            spec = spec + "_" + goods.goods.goods_spec_list[i][j].item_id
        }
      }
    }
    console.log(spec);

    var specs = spec.split("_");
    for (var i = 0; i < specs.length; i++) {
      specs[i] = parseInt(specs[i])
    }
    specs.sort(function(a, b) {
      return a - b
    });
    spec = ""
    for (var i = 0; i < specs.length; i++) {
      if (spec == "")
        spec = specs[i]
      else
        spec = spec + "_" + specs[i]
    }
    console.log(spec);
    var price = goods['spec_goods_price'][spec].price;
    console.log(price);
    this.setData({
      price: price
    });
  },
  bug: function() {

    var bind = getApp().globalData.isband;
    console.log(bind)
    if (!bind) {
      wx.showToast({
        title: '请先绑定手机号',
        icon: 'none'
      })
      setTimeout(function(e) {
        wx.navigateTo({
          url: '/pages/register/index',
        })
      }, 1500)
      return;
    }
    var goods = this.data.goods;
    var app = getApp()
    var that = this;
    var goods_id = that.data.groupInfo.goods_id;
    var info = wx.getStorageSync("sysinfo");
    var session_id = info.unionid; //that.data.goods.goods.spec_goods_price
    var goods_num = that.data.goods_num;

    var user_id = "0"
    if (app.globalData.login)
      user_id = app.globalData.userInfo.user_id

    server.getJSON('/Cart/goodsSale', {
      goods_id: goods_id,
      goods_spec: '',
      session_id: session_id,
      goods_num: goods_num,
      user_id: user_id,
      order_type: 2,
      book_level: 1
    }, function(res) {
      if (res.data.status == 1) {
        wx.navigateTo({
          url: '/pages/order/ordersubmit/index?order_type=2&book_level=1'
        });
      } else
        wx.showToast({
          title: res.data.msg,
          icon: 'error',
          duration: 2000
        });

    });
  },

  sale: function(e) {
    console.log(e)
    var bind = getApp().globalData.isband;
    console.log(bind)
    if (!bind) {
      wx.showToast({
        title: '请先绑定手机号',
        icon: 'none'
      })
      setTimeout(function(e) {
        wx.navigateTo({
          url: '/pages/register/index',
        })
      }, 1500)
      return;
    }
    var goods = this.data.goods;
    var app = getApp()
    var that = this;
    var goods_id = that.data.groupInfo.goods_id;
    var info = wx.getStorageSync("sysinfo");
    var session_id = info.unionid; //that.data.goods.goods.spec_goods_price
    var goods_num = that.data.goods_num;

    var user_id = "0"
    if (app.globalData.login)
      user_id = app.globalData.userInfo.user_id
    var orderid = e.currentTarget.dataset.orderid;
    server.getJSON('/Cart/goodsSale', {
      goods_id: goods_id,
      goods_spec: '',
      session_id: session_id,
      goods_num: goods_num,
      user_id: user_id,
      order_type: 2,
      book_level: 1,
      orderid: orderid
    }, function(res) {
      if (res.data.status == 1) {
        wx.navigateTo({
          url: '/pages/order/ordersubmit/index?order_type=2&book_level=2&book_son=' + orderid
        });
      } else
        wx.showToast({
          title: res.data.msg,
          icon: 'none',
          duration: 2000
        });

    });
  },

  addCart: function() {
    //     var bind = getApp().globalData.isband;
    //     if (!bind) {
    //       console.log("假的")
    //       getApp().isBindPhone();
    //     }
    // 		var goods = this.data.goods;
    // 		var app = getApp()
    // 		var that = this;
    //     console.log(that.data.goods);
    //     // return;
    //     var goods_id = that.data.groupInfo.goods_id;
    // 		var session_id = app.globalData.openid//that.data.goods.goods.spec_goods_price
    // 		var goods_num = that.data.goods_num;
    // 		var user_id = "0"
    //         if(app.globalData.login)
    // 		    user_id = app.globalData.userInfo.user_id

    //        server.getJSON('/Cart/addCart',{goods_id:goods_id,goods_spec:'',session_id:session_id,goods_num:goods_num,user_id:user_id},function(res){
    // if(res.data.status == 1)
    //            wx.showToast({
    // 			title: '已加入购物车',
    // 			icon: 'success',
    // 			duration: 1000
    // 		});
    // 		else
    // 		wx.showToast({
    // 			title: res.data.msg,
    // 			icon: 'error',
    // 			duration: 1000
    // 		});
    // 	   });

    var bind = getApp().globalData.isband;
    console.log(bind)
    if (!bind) {
      wx.showToast({
        title: '请先绑定手机号',
        icon: 'none'
      })
      setTimeout(function(e) {
        wx.navigateTo({
          url: '/pages/register/index',
        })
      }, 1500)
      return;
    }
    var goods = this.data.goods;
    var app = getApp()
    var that = this;
    var goods_id = that.data.groupInfo.goods_id;
    var info = wx.getStorageSync("sysinfo");
    var session_id = info.unionid; //that.data.goods.goods.spec_goods_price
    var goods_num = that.data.goods_num;

    var user_id = "0"
    if (app.globalData.login)
      user_id = app.globalData.userInfo.user_id

    server.getJSON('/Cart/goodsSale', {
      goods_id: goods_id,
      goods_spec: '',
      session_id: session_id,
      goods_num: goods_num,
      user_id: user_id,
      order_type: 2,
      book_level: 1
    }, function(res) {
      if (res.data.status == 1) {
        wx.navigateTo({
          url: '/pages/order/ordersubmit/index?pin=pin'
        });
      } else
        wx.showToast({
          title: res.data.msg,
          icon: 'error',
          duration: 2000
        });

    });
  },
  showCartToast: function() {
    wx.showToast({
      title: '已加入购物车',
      icon: 'success',
      duration: 1000
    });
    wx.navigateTo({
      url: '../../../../../../cart/cart'
    });

  },
  previewImage: function(e) {
    wx.previewImage({
      //从<image>的data-current取到current，得到String类型的url路径
      current: this.data.goods.get('images')[parseInt(e.currentTarget.dataset.current)],
      urls: this.data.goods.get('images') // 需要预览的图片http链接列表
    })
  },
  onShareAppMessage: function (res) {
    var that = this;
    var uid = 0;
    if (getApp().globalData.userInfo !== null) {
      uid = getApp().globalData.userInfo.user_id;
    }
    return {
      title: that.data.groupInfo.goods_name,
      path: '/pages/goods/groupDetail/detail?objectId=' + that.data.goodsid + "&first_leader=" + uid,
      imageUrl: that.data.goods.gallery[0].image_url
    }
  },
});